package com.dong.thread.threadbasic;

/**
 * Interrupt 与 isInterrupted
 * 设置标志位 + 查询标志位
 *
 * @author by jiweidong on 2021/11/21.
 */
public class T04_Interrupt_and_isInterrupted {

    public static void main(String[] args) {
        Thread t = new Thread(() -> {

            for (; ; ) {
                // 这是结束线程很优雅的一种方案
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("Thread is interrupted");
                    System.out.println("isInterrupted： " + Thread.currentThread().isInterrupted());
                    break;
                }
            }
        });

        t.start();

        SleepHelper.sleepSeconds(2);

        t.interrupt();

    }
}
